#include <bits/stdc++.h>
using namespace std;

class ListNode{
public:
	ListNode* next;
	int data;
};

class Solution {
public:
	ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
		ListNode* curA = headA;
		ListNode* curB = headB;
		int lenA = 0,lenB = 0;
		while(curA != NULL){
			lenA++;
			curA = curA ->next;
		}
		while(curB != NULL){
			lenB++;
			curB = curB->next;
		}
		curA = headA,curB = headB;
		if(lenA < lenB){
			swap(curA,curB);
			swap(lenA,lenB);
		}
		int gap = lenA - lenB;
		while(gap--){
			curA =  curA->next;
		}
		while(curA != NULL){
			if(curA == curB) return curA;
			curA = curA ->next;
			curB = curB -> next;
		}
		return NULL;
	}
};

class Test{
public:
	ListNode *getIntersectionNode(ListNode *headA, ListNode *headB){
		ListNode* curA = headA;
		ListNode* curB = headB;
		int cntA = 0;
		while(curA){
			cntA++;
			curA = curA->next;
		}
		int cntB = 0;
		while(curB){
			cntB++;
			curB = curB->next;
		}
		
		curA = headA;
		curB = headB;
		if(cntA < cntB){
			swap(cntA,cntB);
			swap(curA,curB);
		}
		int offset =  cntA - cntB;
		while(offset--){
			curA = curA->next;
		}
		while(curA != NULL && curB != NULL){
			if(curA == curB){
				return curA;
			}
			curA = curA->next;
			curB = curB->next;
		}
		return NULL;
	}
};

int main(){
	
	
	return 0;
}
